package com.leyou.trade.web;

import com.leyou.trade.entity.CartItem;
import com.leyou.trade.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    private CartService cartService;

    @PostMapping
    public ResponseEntity<Void> addCart(@RequestBody CartItem cartItem){
        this.cartService.addCart(cartItem);
        return ResponseEntity.noContent().build();
    }

    @GetMapping("/list")
    public ResponseEntity<List<CartItem>> findCarts(){
        return ResponseEntity.ok(cartService.findCarts());
    }

    @PutMapping
    public ResponseEntity<Void> updateNumber(
            @RequestParam("id") Long skuId,@RequestParam("num") Integer num){
        cartService.updateNumber(skuId,num);
        return ResponseEntity.noContent().build();
    }

    @DeleteMapping("/{skuId}")
    public ResponseEntity<Void> deleteCart(@PathVariable("skuId")Long skuId){
        cartService.deleteCart(skuId);
        return ResponseEntity.noContent().build();
    }

    @PostMapping("/list")
    public ResponseEntity<Void> saveAll(@RequestBody List<CartItem> cartItems){
        cartService.saveAll(cartItems);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
}
